package com.springcloud.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.order.openfeign.StockClient;
import com.springcloud.order.openfeign.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    StockClient stockClient;
    @Autowired
    UserClient userClient;

    @HystrixCommand(fallbackMethod = "fallbackMethod",
                    commandProperties = {
                        @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
                            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "200"),
                            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
                            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")
                    },
                    threadPoolProperties = {
                        @HystrixProperty(name = "coreSize", value = "1"),
                            @HystrixProperty(name = "maxQueueSize", value = "10"),
                            @HystrixProperty(name = "keepAliveTimeMinutes", value = "1000"),
                            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "8"),
                            @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
                            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1500")
                    })
    @RequestMapping("/createOrder")
    public String createOrder() {
        String r = stockClient.reduce(1);
        String result = userClient.getUser("1");
        log.info("调用结果：{},{}",r,result);
        return "订单创建成功";
    }

    public String fallbackMethod(){
        return "当前系统繁忙，请稍后再试";
    }

}
